
//配置信息
import 'package:flutter/material.dart';
import 'package:xreader/base/model/base_model.dart';
import 'package:xreader/logs/log.dart';
import 'package:xreader/novel/entity/entity_novel_book_info.dart';
import 'package:xreader/reader/split/content_split_util.dart';

class ParseConfig{
  double contentWidth;
  double contentHeight;
  double fontSize;
  double lineHeight;
  int pageIndex;
  ParseConfig({
    required this.contentHeight,
    required this.contentWidth,
    this.fontSize = 16,
    this.lineHeight = 32,
    this.pageIndex = 0
  });
}

// 加载 reader 章节内容
abstract class NovelChapterContentModel extends BaseModel{

  //加载章节文本信息
  Future<NovelChapterInfo?> loadNovelChapter({
    required NovelChapterInfo chapterInfo,
    required double contentWidth,
    required double contentHeight
  }) async {

    String? content = "";//加载章节内容
    if(chapterInfo.chapterId != null){
      int chapterId = chapterInfo.chapterId ?? 0;
      content = await queryCachedNovelChapterContent(chapterId: chapterId);
      if(content == null || content.length == 0){
        content = await getNovelChapterContent(chapterId: chapterId);
        cacheContent(chapterContent: content, chapterId: chapterId);
      }
    }
  }

  //解析章节文本内容，不翻页模式有不同解析方式
  Future<NovelChapterInfo> parseChapterContent({
    required String? content,
    required NovelChapterInfo chapterInfo,
    required ParseConfig config
  });

  //获取章节内容
  Future<String> getNovelChapterContent({required int chapterId});

  //从缓存获取章节内容
  Future<String> queryCachedNovelChapterContent({required int chapterId});

  Future<bool> cacheContent({required String chapterContent,required int chapterId});

}

class NetNovelContentModel extends NovelChapterContentModel{
  @override
  Future<bool> cacheContent({required String chapterContent, required int chapterId}) {
    Log.i("缓存====chapterId=${chapterId}");
    return Future.value(true);
  }

  @override
  Future<String> getNovelChapterContent({required int chapterId}) {
    Log.i("获取chapterId=${chapterId} 内容----》");
    StringBuffer sb = new StringBuffer("沉思的手机号是大多数都是的身上的，\n");
    for(int i = 1;i<=1000;i++){
      sb.write("测试的速度的就是看电视看但是${i+1},\n的时间撒大苏打实打实");
    }
    return Future.value(sb.toString());
  }

  @override
  void init() {
    // TODO: implement init
    Log.i("============> init----->");
  }

  @override
  Future<NovelChapterInfo> parseChapterContent(
      {required String? content,
        required NovelChapterInfo chapterInfo,
        required ParseConfig config}) async {
      var titleStyle = TextStyle(
        color: Colors.black,
        fontSize: config.fontSize * 2,
        height: config.lineHeight / config.fontSize,
      );
      var contentStyle = TextStyle(
        color: Colors.black,
        fontSize: config.fontSize,
        height: config.lineHeight / config.fontSize
      );
      //解析
      if(content != null && content.isNotEmpty){
          return await ContentSplitUtil.calculateChapter(
              chapterContent: TextSpan(
                text: chapterInfo.chapterTitle,
                children: [TextSpan(text: content,style: contentStyle)],
                style: titleStyle,
              ),
              contentHeight: config.contentHeight,
              contentWidth: config.contentWidth,
              paragraphSpacing: 10.0,
              currentIndex: config.pageIndex,
          );
      }
      return Future.error("内容为空");
  }

  @override
  Future<String> queryCachedNovelChapterContent({required int chapterId}) {
    Log.i("查询缓存-----chapterId===> ${chapterId}");
    return Future.value("");
  }
  
}


